package day5;

import day4.TestObject;
import sun.awt.SunHints;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

public class Homework1 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException {
        //System.out.println(demo1(5));
        User u=new User("李四",18,"男");
        Student s=new Student();
        demo2();
        //demo3(u,s);
    }
    public static int demo1(int key){
        int[] arr=new int[]{1,2,3,4,5,6};
        int left=0;
        int right=arr.length-1;
        int mid;
        while(left<=right){
            mid=(left+right)/2;
            if(key<arr[mid]){
                right=mid-1;
            }else if(key>arr[mid]){
                left=mid+1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static void demo2() throws ClassNotFoundException {
        //1.获取类对象
        Class c=String.class;
        Method[] ms=c.getDeclaredMethods();//获取全部方法
        //Arrays.sort(ms,(m1,m2)->m1.getName().compareToIgnoreCase(m2.getName()));
        System.out.println(ms.length);
        System.out.println("按字母排序后前5个方法名为：");
        //比较器对象：comparator 目的是用于告诉集合或者数组按照什么属性进行排序 基本类型和String
        //一般都可以默认排序 Comparator==>int compare(T t,T t2)
        //返回值interesting用于表示大小 >0 <0 =0
        /*Comparator<Method> com=(o1,o2)->{
            return o1.getName().compareTo(o2.getName());
        };
        Arrays.sort(ms,com);
        System.out.println(Arrays.toString(ms));*/
        ArrayList<String> top5=new ArrayList<>();
        for(Method m:ms){
            top5.add(m.getName());//两行代码变一行
            //String name=m.getName();
            //top5.add(name);
            //System.out.println(m+""+ m.getName());
        }
        Collections.sort(top5);//默认按照String排序
        for(int i=0;i<5;i++){
            System.out.println(top5.get(i));
        }
    }
    public static void demo3(Object user,Object student) throws IllegalAccessException {
        //1.获取两个类的class
        Class c=user.getClass();
        Class c1=student.getClass();
        Field[] fs=c.getDeclaredFields();//获取user中所有的属性

        for(Field f:fs){
            String fieldName=f.getName();
            f.setAccessible(true);
            try {
                Field studentField=c1.getDeclaredField(fieldName);
                studentField.setAccessible(true);
                //Object value=;
                studentField.set(student,f.get(user));
            } catch (IllegalAccessException e) {
                continue;
            } catch (NoSuchFieldException e) {
                continue;
            }
        }
        Field[] f2=c1.getDeclaredFields();
        for(Field field:f2){
            field.setAccessible(true);
            String name=field.getName();
            Object value=field.get(student);
            System.out.println("属性:"+name+"属性值"+value);
        }
    }

}
class User{
    String name;
    protected int age;
    String sex;

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public User(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}
class Student{
    private String name;
    protected int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
//自定义泛型
class My<A>{
    //类型 参数 不太确定 想统一规范
    public void test(A a,A a2){

    }

    public static void main(String[] args) {
        My<String> m=new My<>();
        m.test("12","Nihao");
        My<Integer> m1=new My();
        m1.test(17,19);
        Integer[] nums={1,2,3,4,5,6};
        Arrays.sort(nums);//默认数字按升序排序
        Comparator<Integer> com=(o1,o2)-> o1-o2;//如果要降序则是o2-o1
        Arrays.sort(nums,com);//降序
        System.out.println(Arrays.toString(nums));
        //System.out.println(m+""+m1);
    }
}
